package co.fitstart.mobile.web.controller.profile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.wechat.web.interceptor.WechatJsConfig;

import co.fitstart.entity.order.Order;
import co.fitstart.entity.order.OrderType;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.agent.Agent;
import co.fitstart.entity.user.agent.AgentDiscount;
import co.fitstart.entity.user.agent.AgentRecord;
import co.fitstart.mobile.service.OrderService;
import co.fitstart.mobile.service.UserAgentService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping(value = "/profile-online-agent")
public class ProfileOnlineAgentController {
	
	@Autowired
	private UserAgentService userAgentService;
	
	@Autowired
	private OrderService orderService;
	
	/**
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	@Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
	public ModelAndView index(@Secure User user) {
		
		ModelAndView modelAndView = new ModelAndView("/profile/online-agent/profile-agent");
		
		Agent agent = userAgentService.get(user);
		modelAndView.addObject("agent", agent);
		
		List<AgentDiscount> agentDiscounts = userAgentService.listDiscount(agent);
        modelAndView.addObject("agentDiscounts", agentDiscounts);
		
		return modelAndView;
	}
	
	/**
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/account", method = RequestMethod.GET)
	@Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
	public @ResponseBody String editAccount(@Secure User user) {
		
		Result result = new Result();
		
		try {
			
			Agent agent = userAgentService.get(user);
			
			result.success().data(agent.describe());
			
		} catch (Exception e) {
			
			result.fail().message("获取账户信息失败");
		}
		
		return JsonUtils.toJsonString(result);
	}
	
	/**
	 * 
	 * @param user
	 * @param account
	 * @param realName
	 * @return
	 */
	@RequestMapping(value = "/account", method = RequestMethod.PATCH)
	@Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
	public @ResponseBody String onEditAccount(@Secure User user,
	                                          @RequestParam("account") String account,
	                                          @RequestParam("realName") String realName) {
		
		Result result = new Result();
		
		try {
			
			Agent agent = userAgentService.get(user);
			userAgentService.editAccount(agent, account, realName);
			
			result.success().message("编辑账户信息成功");
			
		} catch (Exception e) {
			
			result.fail().message("编辑账户信息失败");
		}
		
		return JsonUtils.toJsonString(result);
	}
	
	/**
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/student", method = RequestMethod.GET)
	@Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
	public ModelAndView agentStudent(@Secure User user) {
	    ModelAndView modelAndView = new ModelAndView("profile/online-agent/profile-agent-student");
	    
	    Agent agent = userAgentService.get(user);
	    
	    List<Order> orders = new ArrayList<Order>();
	    if(agent != null) {
	        orders = orderService.listAgentOrder(OrderType.ONLINE, agent);
	    }
        modelAndView.addObject("orders", orders);
	    
	    return modelAndView;
	}
	
	/**
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "/agent-record", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView agentRecord(@Secure User user) {
        ModelAndView modelAndView = new ModelAndView("profile/online-agent/profile-agent-record");
        
        Agent agent = userAgentService.get(user);
        modelAndView.addObject("agent", agent);
        
        List<AgentRecord> agentRecords = userAgentService.listAgentRecord(agent);
        modelAndView.addObject("agentRecords", agentRecords);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "/agent-record/withdraw", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String withdraw(@Secure User user) {
        
        Result result = new Result();
        
        try {
            
            Agent agent = userAgentService.get(user);
            
            result.success().data(agent.withdrawDescribe());
            
        } catch (Exception e) {
            
            result.fail().message("获取提现信息失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param user
     * @param offer
     * @return
     */
    @RequestMapping(value = "/agent-record/withdraw", method = RequestMethod.PATCH)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String onWithdraw(@Secure User user,
                                           @RequestParam("offer") BigDecimal offer) {
        
        Result result = new Result();
        
        try {
            
            Agent agent = userAgentService.get(user);
            
            userAgentService.withdraw(agent, offer);
            
            result.success().message("提现申请提交成功");
            
        } catch (Exception e) {
            
            result.fail().message("提现申请提交失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
	
}
